﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System;
using System.IO;
namespace WPFfile
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>


        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();

            }

            private void InitializeTreeView()
            {
                // Get user's directory
                string userDirectory = @"../";

                // Create root node
                TreeNode rootNode = new TreeNode(userDirectory);
                rootNode.Tag = userDirectory;
                treeView2.Nodes.Add(rootNode);

                // Populate tree with subdirectories
                PopulateTreeView(rootNode, userDirectory);
            }

            private void PopulateTreeView(TreeNode parentNode, string directoryPath)
            {
                try
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
                    foreach (var subDirectory in directoryInfo.GetDirectories())
                    {
                        TreeNode subNode = new TreeNode(subDirectory.Name);
                        subNode.Tag = subDirectory.FullName;
                        parentNode.Nodes.Add(subNode);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error populating tree view: {ex.Message}");
                }
            }

            private void treeView2_AfterSelect(object sender, TreeViewEventArgs e)
            {
                // Clear list view
                listView2.Items.Clear();

                // Get selected directory
                string selectedDirectory = e.Node.Tag.ToString();

                try
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo(selectedDirectory);
                    foreach (var file in directoryInfo.GetFiles())
                    {
                        if (file.Extension == ".txt")
                        {
                            // Add .txt files to list view
                            ListViewItem item = new ListViewItem(file.Name);
                            item.SubItems.Add(file.FullName);
                            listView2.Items.Add(item);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error reading files: {ex.Message}");
                }
            }

            private void listView2_DoubleClick(object sender, EventArgs e)
            {
                // Open selected .txt file
                if (listView2.SelectedItems.Count > 0)
                {
                    string filePath = listView2.SelectedItems[0].SubItems[1].Text;
                    Process.Start(filePath);
                }
            }

            private void MainForm_Load(object sender, EventArgs e)
            {
                InitializeTreeView();
            }

            private void 创建新文件ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                using (var dialog = new SaveFileDialog())
                {
                    dialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        // Create a new empty text file
                        File.Create(dialog.FileName);
                    }
                }
            }

            private void toolStripButton1_Click(object sender, EventArgs e)
            {
                if (listView2.SelectedItems.Count > 0)
                {
                    string filePath = listView2.SelectedItems[0].SubItems[1].Text;
                    Clipboard.SetText(filePath);
                    MessageBox.Show($"File path copied to clipboard:\n{filePath}");
                }
                else
                {
                    MessageBox.Show("Please select a file to copy.");
                }
            }

        private void treeView2_Initialized(object sender, EventArgs e)
        {

        }
    }

    
}